﻿using System;
using System.Web;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfUsrDrawing : msBsc.Cls.BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
            }

        }

        private void BindControls()
        {
            if (!IsPostBack)
            { 
            }
        }

        private DataSet GetDataSet()
        {
            using (msBsc.AppLogic.DataAlc.CBizShpDrawPub opub = new AppLogic.DataAlc.CBizShpDrawPub())
            {
                DataSet ds = opub.DataObj.QueryBySQL(@" 
    SELECT  
    A.DRAW_PUB_ID
    ,DRAW_TITLE=CASE WHEN ISNULL(A.DRAW_TITLE,'')='' THEN B.PRIZE_NME ELSE A.DRAW_TITLE END 
    ,PARTAKE_DT_S=STUFF(STUFF(CONVERT(char(8),A.PARTAKE_DT_S,112),5,0,N'年'),8,0,N'月')+N'日 ' + CONVERT(varchar(8), A.PARTAKE_DT_S, 24)
    ,PARTAKE_DT_E=STUFF(STUFF(CONVERT(char(8),A.PARTAKE_DT_E,112),5,0,N'年'),8,0,N'月')+N'日 ' + CONVERT(varchar(8), A.PARTAKE_DT_E, 24)
    ,DRAWING_DT=STUFF(STUFF(CONVERT(char(8),A.DRAWING_DT,112),5,0,N'年'),8,0,N'月')+N'日 ' + CONVERT(varchar(8), A.PARTAKE_DT_E, 24)
    ,DRAWING_EXPIRE_DT=STUFF(STUFF(CONVERT(char(8),A.DRAWING_EXPIRE_DT,112),5,0,N'年'),8,0,N'月')+N'日'  
	,DRAWING_STATUS=CASE WHEN  A.PARTAKE_DT_S>GETDATE()  THEN '未开启' ELSE 
						(CASE WHEN  A.PARTAKE_DT_E>GETDATE()  THEN '正在抽奖' ELSE 
							(CASE WHEN  A.DRAWING_DT>GETDATE() THEN '等待开奖' ELSE 
								(CASE WHEN DATEDIFF(DAY,A.DRAWING_EXPIRE_DT,GETDATE())<0 THEN '兑奖中' ELSE 
									'已关闭'
								END) 
							END) 
						END)
					END
    ,DRAW_TITLE_IMG_URL=CASE WHEN ISNULL(A.DRAW_TITLE_IMG_URL,'')='' THEN B.PRIZE_IMG_URL ELSE A.DRAW_TITLE_IMG_URL END
	,PARTAKE_COUNT=ISNULL(V2.PARTAKE_COUNT,0)
    FROM SHP_DRAW_PUB A   
	LEFT OUTER JOIN SHP_DRAW_PRIZE B ON A.DRAW_PUB_ID=B.DRAW_PRIZE_ID AND B.SEQ_NUM=0
	LEFT OUTER JOIN 
	(
	SELECT DRAW_PUB_ID,PARTAKE_COUNT=COUNT(*) FROM SHP_DRAW_PARTAKE GROUP BY DRAW_PUB_ID
	) AS V2 ON A.DRAW_PUB_ID=V2.DRAW_PUB_ID
    ORDER BY A.PARTAKE_DT_S DESC
");
                return ds;
            }
        }

        public string GetDataListJavaScript(string CurrPageNum, string strPageRowsCount, string strSort)
        {
            string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(GetDataSet().Tables[0], "", ""
                , (CurrPageNum == string.Empty ? 1 : int.Parse(CurrPageNum))
                , (strPageRowsCount == string.Empty ? 20 : int.Parse(strPageRowsCount))
                , (strSort == string.Empty ? "PARTAKE_DT_S DESC " : strSort));

            return " <script type=\"text/javascript\">var topFormData=" + strjsontem1 + "</script>";
        }

        private void loadlist(string CurrPageNum, string strPageRowsCount, string strSort)
        {
            string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(GetDataSet().Tables[0], "", ""
                , (CurrPageNum == string.Empty ? 1 : int.Parse(CurrPageNum))
                , (strPageRowsCount == string.Empty ? 20 : int.Parse(strPageRowsCount))
                , (strSort == string.Empty ? "PARTAKE_DT_S DESC" : strSort));
            this.ExecJScript(@" var data= " + strjsontem1 + @" ; 
                updateTableHtml( data);");
        }

        public override void DoAction()
        {
            if (this.strAction == "doFinderBoughtList")
            {
                loadlist(  this.GetParamHtValue("NEW_PAGE_NUM")
                    , this.GetParamHtValue("PAGE_ROW_COUNT")
                    , this.GetParamHtValue("PAGE_ROW_SORT"));
            }
        }

    }
}